Skip to content

refactor(db): dedupe Z.AI migrations, account row mapping, emoji query (#1135) - #1284

Merged
axisrow merged 1 commit into
mainfrom
ao/tg_content_factory_5863f66be3-63/dedup-db-repos
Jul 27, 2026
Merged

refactor(db): dedupe Z.AI migrations, account row mapping, emoji query (#1135)#1284
axisrow merged 1 commit into
mainfrom
ao/tg_content_factory_5863f66be3-63/dedup-db-repos

Conversation

@axisrow

@axisrow axisrow commented Jul 10, 2026

Copy link
Copy Markdown
Owner

Part of #1135 (axis 5 of #1130). Three clones from the #1135 target list, all genuine duplication (no parity surface involved).

What & how

migrations.py — twin Z.AI base_url rewriters

_migrate_zai_legacy_base_url and _migrate_zai_empty_base_url_to_coding shared an identical load-JSON → walk zai items → save-on-change body (21-line clone, 109 tokens). Extracted _rewrite_zai_base_urls(db, *, should_rewrite, new_base_url, log_message). Each public migration is now a thin caller passing its own predicate + target URL.

  • Both still run only when something changed (unchanged).
  • Each keeps its own provider_registry import + distinct log message.

accounts.py — duplicated Account row mapping + forked SELECT

  • The Account(...) constructor block was verbatim in get_accounts and get_live_usable_accounts. Extracted _account_from_row(row, session_string); the two methods differ only in decrypt-failure handling (raise vs skip), which stays in the caller.
  • get_decrypted_session reimplemented the SELECT-by-id/phone + decrypt. It now delegates to get_session_export, so the row-binding consistency guard (REST: POST /api/accounts/{id}/export-session #1145) — phone and session bound to the same row read — is no longer forked. Returns exported[1] (the session).

messages.pyget_trending_emojis

Two near-identical SQL bodies differing only by a collected_at window fragment. Collapsed into one query with an optional WHERE fragment + parameterized params list.

Verification

  • tests/test_migrations.py + tests/repositories/ (400) green.
  • Accounts / session-export / trending-emojis keyword tests (481 passed, 6 skipped) green.

jscpd

All three clones from the #1135 target list (migrations.py:720↔762, accounts.py two pairs, messages.py) are removed.

Part of #1135 / #1130. Not for merge — review requested.

🤖 Generated with Claude Code

#1135)

migrations.py: the twin Z.AI base_url rewriters (_migrate_zai_legacy_base_url
and _migrate_zai_empty_base_url_to_coding) shared an identical load-JSON /
walk-zai-items / save-on-change body. Extracted _rewrite_zai_base_urls taking
a should_rewrite predicate + target URL; each public migration is now a thin
caller. Both run only when something changed (unchanged), and each keeps its
own provider_registry import + log message.

accounts.py: the Account(...) constructor block was duplicated verbatim in
get_accounts and get_live_usable_accounts. Extracted _account_from_row(row,
session_string); the two methods differ only in how they handle a decrypt
failure (raise vs skip), which stays in the caller. get_decrypted_session
reimplemented as a delegate to get_session_export so the SELECT + decrypt +
row-binding (#1145 consistency) is no longer forked.

messages.py: get_trending_emojis had two near-identical SQL bodies differing
only by a collected_at window fragment. Collapsed into one query with an
optional WHERE fragment + parameterized params list.

All three jscpd clones from the #1135 target list are removed.
migrations/accounts/messages unit + repository tests pass (400 + 481).

Part of #1135 (axis 5 of #1130).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@axisrow
axisrow force-pushed the ao/tg_content_factory_5863f66be3-63/dedup-db-repos branch from 7fdfd7f to bb01b21 Compare July 27, 2026 14:45
@axisrow

axisrow commented Jul 27, 2026

Copy link
Copy Markdown
Owner Author

🔍 Local review — cycle 1/3 (clean)

Reviewed locally via built-in /review + Codex companion (sol/xhigh), no GitHub bots pinged. Reviewed head bb01b219 against base 53001148.

Three MOVE/extraction refactors — both reviewers confirmed semantic equivalence of each:

Reviewer Verdict Findings
Codex (sol/xhigh) approve 0 — "All three extractions preserve the reviewed predicates, same-row session binding and raise-versus-skip behavior, and SQL parameter ordering. Focused runtime probes and Ruff passed."
/review (Claude) clean 0 — predicates mapped 1:1 (is_zai_legacy_anthropic_base_url on raw string; empty = strip+rstrip == ""); _account_from_row body identical; get_decrypted_sessionget_session_export preserves (and shares) the #1145 row-binding guard; trending-emojis ?-bind order intact; f-string interpolates only a literal fragment (no injection).

Notable: get_decrypted_session delegating to get_session_export is a strict improvement — it now shares the #1145 same-row (phone+session) consistency guard that was previously forked/duplicated.

No FIX, no UNVERIFIED, no SKIP — nothing to apply in cleanup. Round bound to bb01b219; PR snapshot intact (author + head + base unchanged since round start).

Tests: test_migrations.py + repositories/ (404) + accounts/emoji keyword (482) green after rebase. ruff clean.

Totals: 0 FIX, 0 SKIP, 0 UNVERIFIED. Review cycle complete — merge is yours to trigger (local mode does not auto-merge).

@axisrow
axisrow merged commit 26e73f4 into main Jul 27, 2026
4 checks passed
@axisrow
axisrow deleted the ao/tg_content_factory_5863f66be3-63/dedup-db-repos branch July 27, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant